package com.mall.service.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.common.CommonPage;
import com.mall.commons.common.CommonResult;
import com.mall.model.domain.DeliveryParamList;
import com.mall.model.domain.OmsMoneyInfoParam;
import com.mall.model.domain.OmsOrder;
import com.mall.model.domain.OmsReceiverInfoParam;
import com.mall.model.vo.OmsOrderVo;
import com.mall.service.order.service.OmsOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OmsOrderController {
    @Autowired
    OmsOrderService omsOrderService;

    /**
     * 获取订单详情：订单信息、商品信息、操作记录
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public CommonResult<OmsOrderVo> getOrderInfo(@PathVariable Long id){
        OmsOrderVo omsOrderVo = omsOrderService.getByIdWithInfo(id);
        return CommonResult.success(omsOrderVo);
    }

    /**
     *查询订单,分页显示
     * @param pageNum
     * @param pageSize
     * @param createTime
     * @param orderSn
     * @param orderType
     * @param receiverKeyword
     * @param sourceType
     * @param status
     * @return
     */
    @GetMapping("/list")
    public CommonResult<CommonPage> getOrder(@RequestParam(defaultValue = "1") Integer pageNum,
                                             @RequestParam(defaultValue = "10") Integer pageSize,
                                             String createTime,String orderSn,Integer orderType,String receiverKeyword,Integer sourceType,Integer status){
        Page<OmsOrder> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<OmsOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(createTime != null, OmsOrder::getCreateTime, createTime);
        queryWrapper.eq(orderSn != null, OmsOrder::getOrderSn, orderSn);
        queryWrapper.eq(orderType != null,OmsOrder::getOrderType,orderType);
        queryWrapper.like(receiverKeyword != null,OmsOrder::getReceiverName,receiverKeyword).or().eq(receiverKeyword != null,OmsOrder::getReceiverPhone,receiverKeyword); //查询收件人姓名或者手机号码是否和receiverKeyword相同
        queryWrapper.eq(sourceType != null,OmsOrder::getSourceType,sourceType);
        queryWrapper.eq(status != null,OmsOrder::getStatus,status);
        omsOrderService.page(page,queryWrapper);
        CommonPage page1 = CommonPage.getPage(page);
        return CommonResult.success(page1);
    }

    /**
     *批量删除订单
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public CommonResult del(@RequestParam List<Long> ids){
        omsOrderService.removeByIds(ids);
        return CommonResult.success("删除成功");
    }

    /**
     * 批量关闭订单
     * @param ids
     * @param note
     * @return
     */
    @PostMapping("/update/close")
    public CommonResult close(@RequestParam List<Long> ids, String note) {
        omsOrderService.updateWithStatusList(ids,note);
        return CommonResult.success("修改成功");
    }


    /**
     * 批量发货
     * @param deliveryParamList
     * @return
     */
    @PostMapping("update/delivery")
    public CommonResult delivery(@RequestBody List<DeliveryParamList> deliveryParamList){
        omsOrderService.updateWithDelivery(deliveryParamList);
        return CommonResult.success("已发货");
    }

    /**
     *修改订单费用信息
     * @param moneyInfoParams
     * @return
     */
    @PostMapping("/update/moneyInfo")
    public CommonResult moneyInfo(@RequestBody OmsMoneyInfoParam moneyInfoParams){
        omsOrderService.updateWithMoneyInfo(moneyInfoParams);
        return CommonResult.success("修改成功");
    }

    /**
     * 备注订单
     * @param id
     * @param note
     * @param status
     * @return
     */
    @PostMapping("/update/note")
    public CommonResult note(Long id,String note,int status){
        OmsOrder order = omsOrderService.getById(id);
        order.setNote(note);
        order.setStatus(status);
        omsOrderService.updateById(order);
        return CommonResult.success("备注成功");
    }

    /**
     * 修改收货人信息
     * @param omsReceiverInfoParams
     * @return
     */
    @PostMapping("/update/receiverInfo")
    public CommonResult receiverInfo(@RequestBody OmsReceiverInfoParam omsReceiverInfoParams){
        omsOrderService.updateWithReceiverInfo(omsReceiverInfoParams);
        return CommonResult.success("修改成功");
    }

}
